package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.commons.Code;
import com.itheima.commons.PageResult;
import com.itheima.commons.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    //注入userService
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result<User> getAll( @PathVariable("page") int page,@PathVariable("size") int size) {
        //异常捕捉测试
//        int i = 0;
       // if (true) throw new BusinessException(new Result("请换台电脑"));
        PageInfo<User> users = userService.getAll(page,size);
        PageResult pageResult = new PageResult(users.getTotal(), users.getPages(),users.getList());

        return new Result(Code.OK,pageResult,"查询成功");

    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<User> get(@PathVariable("id") int id) {
        User user = userService.get(id);
        return new Result<User>(user != null ? Code.OK : Code.ERROR, user, user != null ? "查询成功" : "查询失败");
    }

    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping
    public Result<User> save(@RequestBody User user) {
        boolean b = userService.save(user);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "新增成功" : "新增失败");

    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @PutMapping
    public Result<User> update(@RequestBody User user) {
        boolean b = userService.update(user);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "修改成功" : "修改失败");
    }

    @DeleteMapping("/{ids}")
    public Result<User> deleteByUuids(@PathVariable("ids") String uuids) {

        boolean b = userService.deleteByUuids(uuids);
        return new Result<User>(b ? Code.OK : Code.ERROR, b ? "删除成功" : "删除失败");

    }

    @PostMapping("/login")
    public Result<User> get(String userName, String password) {
        User user = userService.getUsernameAndPassword(userName, password);
        return new Result<User>(user != null ? Code.OK : Code.ERROR, user, user != null ? "查询成功" : "查询失败");
    }


    /**
     * 分页条件查询
     *
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<User> getUsers(@RequestBody User user, @PathVariable("page") int page,@PathVariable("size") int size) {
        //异常捕捉测试
//        int i = 0;
       // if (true) throw new BusinessException(new Result("请换台电脑"));
        PageInfo<User> users = userService.getUsers(user,page,size);
        PageResult pageResult = new PageResult(users.getTotal(), users.getPages(),users.getList());

        return new Result(Code.OK,pageResult,"查询成功");

    }
}
